/**
 * 
 */
package com.p2p.base.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.p2p.base.exception.BizException;

/**
 * @author leiruiqi
 *
 */
public class MyExceptionHandler implements HandlerExceptionResolver {

	private static final Logger logger = LoggerFactory
			.getLogger(MyExceptionHandler.class);

	private String exceptionPage = "errorPage";

	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		logger.error("error", ex);
		ModelAndView view = new ModelAndView(exceptionPage);
		String message = ex.getMessage();
		view.addObject("err", message);
		if (ex instanceof BizException) {
			view.addObject("errtype", 1);
			//TODO 硬编码
			if (StringUtils.isNotBlank(message)) {
				view.addObject("errmsg", message);
			} else {
				view.addObject("errmsg",((BizException) ex).getCode() );
			}
		} else {
			view.addObject("errtype", 0);
		}
		ex.printStackTrace();
		return view;
	}

	public String getExceptionPage() {
		return exceptionPage;
	}

	public void setExceptionPage(String exceptionPage) {
		this.exceptionPage = exceptionPage;
	}

}
